package com.geekgo.lucenepp.util;

import com.geekgo.lucenepp.core.config.datasource.LuceneDataSource;
import com.geekgo.lucenepp.model.Field;
import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.*;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 查询工具
 *
 * @author HeHaoyang
 * @create 2017-06-13 14:12
 **/
public final class QueryUtil {

    /**
     * 获取查询语句
     * @param fields
     * @param value
     * @return
     */
    public static Query getRangeQuery(String[] fields,String value){
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        for(String field : fields){
            TermQuery termQuery = new TermQuery(new Term(field,value));
            builder.add(termQuery, BooleanClause.Occur.SHOULD);
        }
        return builder.build();
    }

    public static Query getInitExactQuery(String field,int value){
        Query query = IntPoint.newExactQuery(field,value);
        return query;
    }

    /**
     * 获取查询字段
     * @param fieldList
     * @return
     */
    public static String[] getQueryFields(List<Field> fieldList){
        Set<String> fieldSet = new HashSet<>();
        for(Field field : fieldList){
            if(field.isQuery()){
                fieldSet.add(field.getName());
            }
        }
        return fieldSet.toArray(new String[fieldSet.size()]);
    }

    /**
     * 查询排序字段处理
     * @param fieldList
     * @return
     */
    public static Sort sortFields(List<Field> fieldList){
        Sort sort = new Sort();
        List<SortField> sortFieldList = new ArrayList<>();
        for(Field field : fieldList){
            if(StringUtils.isNotBlank(field.getSort())){
                SortField sortField = null;
                switch (field.getType()) {
                    case "int":
                        sortField = new SortField(field.getName(), SortField.Type.INT, Boolean.valueOf(field.getSort()));
                        break;
                    case "float":
                        sortField = new SortField(field.getName(), SortField.Type.FLOAT, Boolean.valueOf(field.getSort()));
                        break;
                    case "double":
                        sortField = new SortField(field.getName(), SortField.Type.DOUBLE, Boolean.valueOf(field.getSort()));
                        break;
                    case "string":
                        sortField = new SortField(field.getName(), SortField.Type.STRING, Boolean.valueOf(field.getSort()));
                        break;
                    default:
                        break;
                }
                if(sortField != null){
                    sortFieldList.add(sortField);
                }
            }
        }
        sort.setSort(sortFieldList.toArray(new SortField[sortFieldList.size()]));
        return sort;
    }

    /**
     * 高亮处理
     * @param dataSource
     * @param scorer
     * @return
     */
    public static Highlighter getHighlighter(LuceneDataSource dataSource, QueryScorer scorer){
        Fragmenter fragmenter = new SimpleFragmenter(100);//默认字符为100
        Highlighter highlighter = new Highlighter(dataSource.getSimpleHTMLFormatter(),scorer);
        highlighter.setTextFragmenter(fragmenter);
        return highlighter;
    }
}
